package com.atguigu.tingshu;

import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)// 取消数据源自动配置
@EnableDiscoveryClient
@EnableFeignClients
@EnableAsync
@EnableElasticsearchRepositories(basePackages = "com.atguigu.tingshu.search.dao")
public class ServiceSearchApplication {

    private final RedissonClient redissonClient;

    @Autowired
    public ServiceSearchApplication(RedissonClient redissonClient) {
        this.redissonClient = redissonClient;
    }


    public static void main(String[] args) {
        SpringApplication.run(ServiceSearchApplication.class, args);
    }

    /**
     * 布隆过滤器的初始化
     * 布隆算法：N个hash
     *
     * @return
     */
    @Bean
    public RBloomFilter<Object> rBloomFilter() {
        RBloomFilter<Object> albumBloomFilter = redissonClient.getBloomFilter("album_bloom_filter");
        // 10000次的情况下误差小于0.01
        albumBloomFilter.tryInit(10000, 0.01);
        return albumBloomFilter;
    }
}
